package com.thtf.openguess.util;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ApiResult
 * @Description: REST API 返回结果
 * @Author lisu
 * @Date 2019-10-23
 * @Version V1.0
 **/
@Data
@Accessors(chain = true)
@Builder
@AllArgsConstructor
@ApiModel(value = "ApiResult-返回结果包装实体", description = "返回结果包装实体")
public class ApiResult<T> implements Serializable {

    @ApiModelProperty(value = "编码")
    private int code;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "消息")
    private String msg;


    @ApiModelProperty(value = "时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "YYYY-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date time;

    public ApiResult() {

    }

    public static ApiResult result(boolean flag) {
        if (flag) {
            return ok();
        }
        return fail("");
    }

    public static ApiResult result(ApiCode apiCode) {
        return result(apiCode, null);
    }

    public static ApiResult result(ApiCode apiCode, Object data) {
        return result(apiCode, null, data);
    }

    public static ApiResult result(ApiCode apiCode, String msg, Object data) {
        String message = apiCode.getMsg();
        if (StringUtils.isNotBlank(msg)) {
            message = msg;
        }
        return ApiResult.builder()
                .code(apiCode.getCode())
                .msg(message)
                .data(data)
                .time(new Date())
                .build();
    }

    public static ApiResult result(Integer code, String message, Object data) {
        return ApiResult.builder()
                .code(code)
                .msg(message)
                .data(data)
                .time(new Date())
                .build();
    }

    public static ApiResult ok() {
        return ok(null);
    }

    public static ApiResult ok(Object data) {
        return result(ApiCode.SUCCESS, data);
    }

    public static ApiResult ok(Object data, String msg) {
        return result(ApiCode.SUCCESS, msg, data);
    }

    public static ApiResult okMap(String key, Object value) {
        Map<String, Object> map = new HashMap(1024);
        map.put(key, value);
        return ok(map);
    }

    public static ApiResult fail(ApiCode apiCode) {
        return result(apiCode, null);
    }

    public static ApiResult fail(String msg) {
        return result(ApiCode.FAIL, msg, null);

    }


    public static ApiResult fail(ApiCode apiCode, String msg) {
        if (ApiCode.SUCCESS == apiCode) {
            throw new RuntimeException("失败结果状态码不能为" + ApiCode.SUCCESS.getCode());
        }
        return result(apiCode, msg, null);

    }

    public static ApiResult fail(ApiCode apiCode, Object data) {
        if (ApiCode.SUCCESS == apiCode) {
            throw new RuntimeException("失败结果状态码不能为" + ApiCode.SUCCESS.getCode());
        }
        return result(apiCode, data);

    }

    public static ApiResult fail(String key, Object value) {
        Map<String, Object> map = new HashMap(1024);
        map.put(key, value);
        return result(ApiCode.FAIL, map);
    }

    public static ApiResult fail() {
        return fail(ApiCode.FAIL);
    }
}